﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace DEMI_webapp
{
    public partial class SklepInternetowy : System.Web.UI.Page
    {
        S_Koszyk koszyk;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["koszyk"] == null)
            {
                koszyk = new S_Koszyk();
                Session["koszyk"] = koszyk;
            }
            else
                koszyk = (S_Koszyk)Session["koszyk"];
            Label2.Text = koszyk.PodajIloscKapeluszy().ToString();
            Label4.Text = koszyk.PodajLacznaCene().ToString() + " zł";


            string c = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(c);
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandType = System.Data.CommandType.Text;

            cmd.Connection = con;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                //sprawdzenie aktualnej ilości danego kapelusza
                string cena = row.Cells[2].Text;
                cena = cena.Replace(',', '.');
                cmd.CommandText = "SELECT ilosc FROM Kapelusze WHERE (nazwa = '" + row.Cells[0].Text + "') AND (kolor = '" + row.Cells[1].Text + "') AND (cena = " + cena + ") AND (material = '" + row.Cells[3].Text + "')";
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                rdr.Read();
                int ColID = rdr.GetOrdinal("ilosc");
                int ile = rdr.GetInt32(ColID);
                con.Close();
                if (ile == 0)
                {
                    row.Cells[6].Enabled = false;
                }
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            GridView1.SelectedIndex = -1;
            TextBox txt = (TextBox)row.FindControl("TextBox1");
            if (txt.Text != "")
            {
                string c = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["conString"].ConnectionString;
                System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(c);
                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = con;
                //sprawdzenie aktualnej ilości danego kapelusza
                string cena = row.Cells[2].Text;
                cena = cena.Replace(',', '.');
                cmd.CommandText = "SELECT ilosc FROM Kapelusze WHERE (nazwa = '" + row.Cells[0].Text + "') AND (kolor = '" + row.Cells[1].Text + "') AND (cena = " + cena + ") AND (material = '" + row.Cells[3].Text + "')";
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                rdr.Read();
                int ColID = rdr.GetOrdinal("ilosc");
                int ile = rdr.GetInt32(ColID);
                con.Close();

                if (Convert.ToInt32(txt.Text) <= ile && Convert.ToInt32(txt.Text) > 0)
                {
                    S_Kapelusz temp = new S_Kapelusz(row.Cells[0].Text, row.Cells[1].Text, row.Cells[3].Text, Convert.ToDouble(row.Cells[2].Text), Convert.ToInt32(txt.Text));
                    koszyk.DodajDoKoszyka(temp);
                    Label2.Text = koszyk.PodajIloscKapeluszy().ToString();
                    Label4.Text = koszyk.PodajLacznaCene().ToString() + " zł";
                    txt.Text = "";
                }
            }
        }

        protected void Button1_Click(object sender, ImageClickEventArgs e)
        {

        }
    }
}